function makeIESupportHTML5() {
	document.createElement('header');
	document.createElement('nav');
	document.createElement('section');
	document.createElement('article');
	document.createElement('aside');
	document.createElement('footer');
}

function request(method, url, async, data, callback) {
	var xhr = false;
	try {
		// For Old Microsoft Browsers
		xhr = new ActiveXObject('Msxml2.XMLHTTP');
	} catch (e) {
		try {
			// For Microsoft IE 6.0+
			xhr = new ActiveXObject('Microsoft.XMLHTTP');
		} catch (e2) {
			// No Browser accepts the XMLHTTP Object then false
			xhr = false;
		}
	}
	if (!xhr && typeof XMLHttpRequest != 'undefined') {
		// For Mozilla, Opera Browsers
		xhr = new XMLHttpRequest();
	}
	if (xhr) {
		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4) {
				callback(xhr);
			}
		}
		xhr.open(method, url, async);
		// set this header to send POST
		xhr.setRequestHeader('Content-type',
				'application/x-www-form-urlencoded');
		xhr.send(data);
	}
}

function populateHome() {
//	document.getElementById('content').innerHTML += "<div id='intro' class='main'></div>";
//	var html = "<article>";
//	html += "<header>";
//	html += "<aside class='img'><img src='https://googledrive.com/host/0BwPdwaRwADWMM21jd3F6aDJobmM'/></aside>";
//	html += "<h1>Gracefully Degrading During Scheduled Maintenance</h1>";
//	html += "</header>";
//	html += "<p>One of the joys of keeping anything you do as simple as possible";
//	html += "is making your achievement yours. Once you begin to make something,";
//	html += "for example a website, you should learn from others. And you should";
//	html += "get experience from many of course. Later your avarious thinkings";
//	html += "will make your website become too more complex to control and";
//	html += "remember all of its aspects. So at the begining, let keep it simple";
//	html += "in mind, you shall not miss what are best while keeping control on";
//	html += "what you are doing.</p>";
//	html += "<p>Do you alway keep your things simple? And how do you keep them simple?";
//	html += "Uhm, that is your work. Yuh, my work is on affecting your opinions";
//	html += "about simplize. Hope my simple brain's topics do not take you much";
//	html += "wasting time.</p>";
//	html += "</article>";
//	document.getElementById('intro').innerHTML += html;
	request('GET', 'http://inkisapi.appspot.com/v1/posts', true, null, function(xhr) {
		document.getElementById('content').innerHTML += "<div id='main' class='main left'></div>";
		if (xhr.status == 200) {
			try {
				var jsonData = eval('(' + xhr.responseText + ')');
				var output = "";
				for (i=0; i<jsonData.length; i++){
					output += "<article>";
					output += "<header>";
					output += "<aside>" + jsonData[i].date.substring(0, 12) + "</aside>";
					output += "<h1><a href='archives.html?id=" + jsonData[i].id + "'>" + jsonData[i].title + "</a></h1>";
					output += "<p class='author'>by <a href='archives.html?author=" + jsonData[i].author + "'>" + jsonData[i].author + "</a></p>";
					output += "</header>";
					output += "<p>" + jsonData[i].meta + "</p>";
					output += "</article>";
				}
				document.getElementById('main').innerHTML = output;
			} catch (e) {
				document.getElementById('main').innerHTML = e;
			}
		} else {
			document.getElementById('main').innerHTML = "<p>Server problem. Try refresh your browser.</p>";
		}
	});
}

function populateArchives() {
	var search = false;
	var req = window.location.search;
	var author = getUrlVars()["author"];
	var id = getUrlVars()["id"];
	var url = null;
	if (req === null || req === '' || req === 'undefined'){
		url = 'http://inkisapi.appspot.com/v1/posts';
		request('GET', url, true, null, function(xhr) {
			document.getElementById('content').innerHTML += "<div id='main' class='main'></div>";
			if (xhr.status == 200) {
				try {
					var jsonData = eval('(' + xhr.responseText + ')');
					var output = "";
					for (i=0; i<jsonData.length; i++){
						output += "<article>";
						output += "<header>";
						if (i == 0 || i == 3 || i == 7){
							output += "<aside>2013</aside>";
						}
						output += "<p class='meta'>" + jsonData[i].date + "</p>";
						output += "<h1><a href='archives.html?id=" + jsonData[i].id + "'>" + jsonData[i].title + "</a></h1>";
						output += "<p class='author'>by <a href='archives.html?author=" + jsonData[i].author + "'>" + jsonData[i].author + "</a></p>";
						output += "</header>";
						output += "</article>";
					}
					document.getElementById('main').innerHTML = output;
				} catch (e) {
					document.getElementById('main').innerHTML = e;
				}
			} else {
				document.getElementById('main').innerHTML = "Server problem. Try refresh your browser.";
			}
		});
	} else if (id != null){
		url = 'http://inkisapi.appspot.com/v1/posts/' + id;
		request('GET', url, true, null, function(xhr) {
			document.getElementById('content').innerHTML += "<div id='main' class='main'></div>";
			document.getElementById('content').innerHTML += "<div id='comments' class='main'></div>";
			if (xhr.status == 200) {
				try {
					var jsonData = eval('(' + xhr.responseText + ')');
					var output = "";
					output += "<article>";
					output += "<header>";
					output += "<aside>77 777 7777</aside>";
					output += "<p class='meta'>" + jsonData.date + "</p>";
					output += "<h1><a href='archives.html?id=" + jsonData.id + "'>" + jsonData.title + "</a></h1>";
					output += "<p class='author'>by <a href='archives.html?author=" + jsonData.author + "'>" + jsonData.author + "</a></p>";
					output += "</header>";
					output += "<p>" + jsonData.meta + "</p>";
					output += "<section>" + jsonData.content + "</section>";
					output += "</article>";
					document.getElementById('main').innerHTML = output;
				} catch (e) {
					document.getElementById('main').innerHTML = e;
				}
			} else {
				document.getElementById('main').innerHTML = "Server problem. Try refresh your browser.";
			}
		});
	} else if (author != null){
		url = 'http://inkisapi.appspot.com/v1/posts?author=' + author;
		request('GET', url, true, null, function(xhr) {
			document.getElementById('content').innerHTML += "<div id='main' class='main'></div>";
			if (xhr.status == 200) {
				try {
					var jsonData = eval('(' + xhr.responseText + ')');
					var output = "";
					for (i=0; i<jsonData.length; i++){
						output += "<article>";
						output += "<header>";
						if (i == 0 || i == 3 || i == 7){
							output += "<aside>2013</aside>";
						}
						output += "<p class='meta'>" + jsonData[i].date + "</p>";
						output += "<h1><a href='archives.html?id=" + jsonData[i].id + "'>" + jsonData[i].title + "</a></h1>";
						output += "<p class='author'>by <a href='archives.html?author=" + jsonData[i].author + "'>" + jsonData[i].author + "</a></p>";
						output += "</header>";
						output += "<p>" + jsonData[i].meta + "</p>";
						output += "</article>";
					}
					document.getElementById('main').innerHTML = output;
				} catch (e) {
					document.getElementById('main').innerHTML = e;
				}
			} else {
				document.getElementById('main').innerHTML = "Server problem. Try refresh your browser.";
			}
		});
	}
}

function getUrlVars() {
	var vars = {};
	var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
		vars[key] = value;
	});
	return vars;
}